<?php
include './config/config.php';

$sql = "SELECT * FROM `pre_job` WHERE `id` = {$_GET['jobId']}";
$data = query($sql);

$depSql = "SELECT * FROM `pre_department` ORDER BY `id` ASC";
$depList = queryAll($depSql);


if ($_POST) {
  // todo 检查是否存在
  if ($_POST['action'] == 'check') {
    $val = $_POST['val'];
    $id = $_POST['id'];

    $sql = "SELECT * FROM `pre_job` WHERE `id` != '$id'";

    $dataList = queryAll($sql);

    foreach ($dataList as $item) {
      if ($val == $item['name']) {
        error('用户名已存在');
        break;
      }
    }
    success('用户名可以使用');
  }

  $formData = array(
    "name" => $_POST['name'],
    "depid" => $_POST['depid']
  );

  $where = "`id` = '{$_POST['id']}'";
  $res = updataById("job", $formData, $where);

  if ($res) {
    ShowMsg($res['msg'], "jobList.php");
  } else {
    ShowMsg("修改失败");
  }
}
?>

<!DOCTYPE html>
<html lang="en">

<head>
  <?php include_once('common/meta.php'); ?>
</head>

<body>
  <?php include_once('common/header.php'); ?>

  <?php include_once('common/menu.php'); ?>

  <div class="content">
    <div class="header">
      <h1 class="page-title">修改岗位信息</h1>
    </div>
    <ul class="breadcrumb">
      <li><a href="jobList.php">JobList</a> <span class="divider">/</span></li>
      <li class="active">JobUpdata</li>
    </ul>

    <div class="container-fluid">
      <div class="row-fluid">

        <div class="btn-toolbar">
          <button class="btn btn-primary" onClick="location='jobList.php'"><i class="icon-list"></i> 岗位列表</button>
          <div class="btn-group">
          </div>
        </div>

        <div class="well">
          <div id="myTabContent" class="tab-content">
            <div class="tab-pane active in" id="home">

              <form method="POST" id="updata_form">
                <input type="hidden" name="id" value="<?php echo $data['id']; ?>">
                <label for="JobName">岗位名称</label>
                <input id="JobName" type="text" name="name" value="<?php echo $data['name']; ?>">
                <span id="username"></span>

                <label for="DepId">所属部门</label>
                <select name="depid" id="DepId">
                  <?php foreach ($depList as $item) { ?>
                    <option value="<?php echo $item['id'] ?>" <?php echo $item['id'] == $data['depid'] ? 'selected' : '' ?>>
                      <?php echo $item['name']; ?>
                    </option>
                  <?php } ?>
                </select>

                <br>
                <button class="btn btn-primary" type="submit">更新</button>
              </form>
            </div>
          </div>
        </div>

        <?php include_once('common/footer.php'); ?>

      </div>
    </div>
  </div>

  <script src="./assets/lib/jquery-1.8.1.min.js"></script>
  <script>
    // todo 检测用户名
    $('input[name="name"]').change(function(e) {
      let val = $(this).val();
      let id = $("input[name='id']").val();
      $.ajax({
        type: "post",
        url: "jobUpdata.php",
        data: {
          id,
          val,
          action: 'check'
        },
        dataType: "json",
        success: function(res) {
          if (res.code === 1) {
            $("#username").html(res.msg).css({
              "color": "green"
            });
          } else if (res.code === 0) {
            $("#username").html(res.msg).css({
              "color": "red"
            });
          }
        },
        error: function(err) {
          console.log(err);
        }
      });
    })
  </script>
</body>

</html>
<?php include_once('common/script.php'); ?>